//
//  MahjongRecoredTVC.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/30.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongRecoredTVC.h"
#import "PlayerHeadAndNameView.h"

#import "ResultModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

typedef enum : NSUInteger {
    GangType_ZhiGang,
    GangType_AnGang,
    GangType_BuGang,
} GangType;

typedef enum : NSUInteger {
    liushui_angang = 1,             //暗杠
    liushui_beiangang,              //被暗杠
    liushui_bugang,                //补杠
    liushui_beibugang,              //被补杠
    
    liushui_zhigang,               //直杠
    liushui_diangang,               //点杠
    
    liushui_beicagua,               //被擦挂
    
    liushui_beizhuanyu,             //被转雨
    liushui_zhuanyu,                //转雨
    
    liushui_hu,                     //胡
    liushui_dianpao,                //点炮
    
    liushui_gangshangpao,           //杠上炮
    liushui_gangshangdianpao,       //杠上点炮
    
    liushui_gangshanghua,           //杠上花
    liushui_beigangshanghua,        //被杠上花
    liushui_zimo,                   //自摸
    liushui_beizimo,                //被自摸
    
    liushui_chajiao,                //查叫
    liushui_beichajiao,             //被查叫
} MahjongLiushuiType;

@interface MahjongRecoredTVC()

@property (weak, nonatomic) IBOutlet UIView *totalContainer;
@property (weak, nonatomic) IBOutlet UIView *leftContainer;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIView *playerContainer;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *playerNamesLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descLabelLeftConstraints;


@end

@implementation MahjongRecoredTVC

- (void)setActionModel:(ActionModel *)am{
    NSAssert(nil != _owner,@"请先给owner赋值");
    for (UIView *v in _leftContainer.subviews) {
        if (![v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    for (UIView *v in _playerContainer.subviews) {
        if (![v isKindOfClass:[UILabel class]]) {
            [v removeFromSuperview];
        }
    }
    
    MahjongLiushuiType liushui_type = 0; //图标
    GangType gangType = 0;//默认直杠
    NSString *descStr = @""; // 描述
    NSString *scoreStr = @""; // 分数
    NSMutableArray *targetPlayerArr = [NSMutableArray new]; //目标
    
    BOOL isHost = NO;
    if ([am.owner.playerId isEqualToString:_owner.playerId]) {
        isHost = YES;
    }

    if ([am.actionType isEqualToString:@"杠"]) {
        gangType = [am.attributesArr containsObject:@"直杠"]?GangType_ZhiGang:[am.attributesArr containsObject:@"暗杠"]?GangType_AnGang:GangType_BuGang;
        switch (gangType) {
            case GangType_ZhiGang:
                liushui_type = isHost?liushui_zhigang:liushui_diangang;
                scoreStr = isHost?NSStringFormat(@"%ld",am.score + (am.score/2) * am.caguaArr.count):([am.targetPlayerIdArr containsObject:_owner.playerId]?NSStringFormat(@"%ld",am.score):NSStringFormat(@"%ld",am.score/2));
                targetPlayerArr = isHost?[NSMutableArray arrayWithArray:am.targetsArr]:[NSMutableArray arrayWithObject:am.owner];
                //被擦挂
                if ([am.caguaPlayerIdArr containsObject:_owner.playerId]){
                    liushui_type = liushui_beicagua;
                    [targetPlayerArr removeAllObjects];
                    [targetPlayerArr addObject:am.owner];
                }
                break;
            case GangType_AnGang:
                liushui_type = isHost?liushui_angang:liushui_beiangang;
                scoreStr = isHost?NSStringFormat(@"%ld",am.score * am.targetsArr.count):NSStringFormat(@"%ld",am.score);
                targetPlayerArr = isHost?[NSMutableArray arrayWithArray:am.targetsArr]:[NSMutableArray arrayWithObject:am.owner];
                break;
            case GangType_BuGang:
                liushui_type = isHost?liushui_bugang:liushui_beibugang;
                scoreStr = isHost?NSStringFormat(@"%ld",am.score * am.targetsArr.count):NSStringFormat(@"%ld",am.score);
                targetPlayerArr = isHost?[NSMutableArray arrayWithArray:am.targetsArr]:[NSMutableArray arrayWithObject:am.owner];
                break;
            default:
                break;
        }
        
        if (am.returned) {
            descStr = @"已返雨";
            scoreStr = @"0";
        }else if(am.transferedTo){
            descStr = @"转雨";
            
            if (isHost) {//action主人分数为0
                scoreStr = @"0";
            }else if (![_owner.playerId isEqualToString:am.transferedTo.playerId]){
                scoreStr = NSStringFormat(@"%ld",am.score);
                if (am.caguaArr.count && ![am.targetPlayerIdArr containsObject:_owner.playerId]) {
                    scoreStr = NSStringFormat(@"%ld",am.score/2);
                }
            }else if([am.caguaPlayerIdArr containsObject:am.transferedTo.playerId]){
                //有擦挂，只能是直杠
                scoreStr = NSStringFormat(@"%ld",am.score + am.score/2 * MAX((am.caguaArr.count - 1),0));
            }else if([am.targetPlayerIdArr containsObject:am.transferedTo.playerId]){
                if (gangType == GangType_ZhiGang) {
                    scoreStr = NSStringFormat(@"%ld",am.score/2 * am.caguaArr.count);
                }else{
                    scoreStr = NSStringFormat(@"%ld",am.score * MAX((am.targetsArr.count - 1),0));
                }
            }
            
            [targetPlayerArr removeAllObjects];
            [targetPlayerArr addObject:am.transferedTo];
        }
        
    }else if ([am.actionType isEqualToString:@"胡"]) {
        if ([am.attributesArr containsObject:@"自摸"]) {
            liushui_type = isHost?liushui_zimo:liushui_beizimo;
        }else if ([am.attributesArr containsObject:@"杠上花"]){
            liushui_type = isHost?liushui_gangshanghua:liushui_beigangshanghua;
        }else if ([am.attributesArr containsObject:@"杠上炮"]){
            liushui_type = isHost?liushui_gangshangpao:liushui_gangshangdianpao;
        }else {
            liushui_type = isHost?liushui_hu:liushui_dianpao;
        }
        scoreStr = isHost?NSStringFormat(@"%ld",am.score * am.targetsArr.count):NSStringFormat(@"%ld",am.score);
        targetPlayerArr = isHost?[NSMutableArray arrayWithArray:am.targetsArr]:[NSMutableArray arrayWithObject:am.owner];
    }else{//查叫
        liushui_type = isHost?liushui_chajiao:liushui_beichajiao;
        scoreStr = isHost?NSStringFormat(@"%ld",am.score * am.targetsArr.count):NSStringFormat(@"%ld",am.score);
        targetPlayerArr = isHost?[NSMutableArray arrayWithArray:am.targetsArr]:[NSMutableArray arrayWithObject:am.owner];
    }
    
    //图标
    if (liushui_type > 0) {
        UIImage *image = UIImageWithName([self toStr:liushui_type]);
        CGFloat h = _leftContainer.frame.size.height - 10;
        CGFloat imgW = image.size.width;
        CGFloat imgH = image.size.height;
        if (imgH > 0) {
            CGFloat imgWW = h * (imgW/imgH);
            UIImageView *igv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, imgWW, h)];
            igv.image = image;
            [_leftContainer addSubview:igv];
            _descLabelLeftConstraints.constant = imgWW + 5;
        }
    }
    //描述
    _descLabel.text = descStr;
    //分数
    if (![scoreStr isEqualToString:@"0"]) {
        _scoreLabel.textColor = isHost?[UIColor redColor]:UIColorHex(#8BB335);
        _scoreLabel.text = isHost?NSStringFormat(@"+%@",scoreStr):NSStringFormat(@"-%@",scoreStr);
        if ([am.transferedTo.playerId isEqualToString:_owner.playerId]) {
            _scoreLabel.textColor = [UIColor redColor];
            _scoreLabel.text = NSStringFormat(@"+%@",scoreStr);
        }
    }else{
        _scoreLabel.text = scoreStr;
        _scoreLabel.textColor = [UIColor grayColor];
    }
    //目标玩家
    CGFloat w = _playerContainer.frame.size.width;
    for (PlayerModel *pm in targetPlayerArr) {
        for (ResultModel *rm in _gameModel.resultArr) {
            if ([pm.playerId isEqualToString:rm.playerModel.playerId]) {
                NSInteger count = _playerContainer.subviews.count - 1;
                PlayerHeadAndNameView *pv = [PlayerHeadAndNameView playerHeadAndNameView];
                pv.frame = CGRectMake(w - (count+1) * 56 - count*5, 2, 56, 76);
                //昵称
                pv.nameLabel.text = rm.playerModel.playerName;
                //头像
                pv.firstLabel.font = DefaultSystemFont(35);
                if (rm.playerModel.avatar.length) {
                    pv.headImageView.layer.cornerRadius = 28.f;
                    pv.headImageView.layer.masksToBounds = YES;
                    [pv.headImageView sd_setImageWithURL:[NSURL URLWithString:rm.playerModel.avatar] placeholderImage:UIImageWithName(@"default_head")];
                    pv.firstLabel.text = @"";
                }else{
                    pv.firstLabel.text = [rm.playerModel.playerName substringToIndex:1];
                }
                
                [_playerContainer addSubview:pv];
                break;
            }
        }
    }
    
}

- (NSString *)toStr:(MahjongLiushuiType)liushui_type{
    switch (liushui_type) {
        case liushui_angang:
            return @"liushui_angang";
            break;
        case liushui_beiangang:
            return @"liushui_beiangang";
            break;
        case liushui_bugang:
            return @"liushui_bugang";
            break;
        case liushui_beibugang:
            return @"liushui_beibugang";
            break;
        case liushui_zhigang:
            return @"liushui_zhigang";
            break;
        case liushui_diangang:
            return @"liushui_diangang";
            break;
        case liushui_beicagua:
            return @"liushui_beicagua";
            break;
        case liushui_zhuanyu:
            return @"liushui_zhuanyu";
            break;
        case liushui_beizhuanyu:
            return @"liushui_beizhuanyu";
            break;
        case liushui_hu:
            return @"liushui_hu";
            break;
        case liushui_dianpao:
            return @"liushui_dianpao";
            break;
        case liushui_gangshangpao:
            return @"liushui_gangshangpao";
            break;
        case liushui_gangshangdianpao:
            return @"liushui_gangshangdianpao";
            break;
        case liushui_gangshanghua:
            return @"liushui_gangshanghua";
            break;
        case liushui_beigangshanghua:
            return @"liushui_beigangshanghua";
            break;
        case liushui_zimo:
            return @"liushui_zimo";
            break;
        case liushui_beizimo:
            return @"liushui_beizimo";
            break;
        case liushui_chajiao:
            return @"liushui_chajiao";
            break;
        case liushui_beichajiao:
            return @"liushui_beichajiao";
            break;
            
        default:
            return @"";
            break;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    _totalContainer.backgroundColor = UIColorFromRGBA(0xD8D8D8, 0.3);
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
